package labox.innovation.gameserver.model;

import java.util.List;

import javolution.util.FastList;
import labox.innovation.util.Rnd;

/**
 * 掉落列表类描述一个掉落列表的数据
 */
public final class FuzeDropCategory {

	/**
	 * 掉落列表
	 */
	private List<FuzeDropData> _drops;

	/**
	 * 掉落列表的数量模板
	 */
	private List<FuzeDropCategoryDrop> _dropNum;

	/**
	 * 掉落列表概率和
	 */
	private int _categoryChance;

	public FuzeDropCategory() {
		_drops = new FastList<FuzeDropData>();
		_dropNum =new FastList<FuzeDropCategoryDrop>();
	}
	
	public void addDropNum(FuzeDropCategoryDrop drop){
		_dropNum.add(drop);
	}

	public void addDropData(FuzeDropData drop) {
		boolean found = false;

		if (drop.isQuestDrop()) {
			//if (_questDrops == null)
			//	_questDrops = new FastList<L2DropData>(0);
			//_questDrops.add(drop);
		} else {
			if (!found) {
				_drops.add(drop);
				_categoryChance += drop.getChance();
			}
		}
	}

	public List<FuzeDropData> getAllDrops() {
		return _drops;
	}
	
	public List<FuzeDropCategoryDrop> getAllDropNum(){
		return _dropNum;
	}

	public void clearAllDrops() {
		_drops.clear();
		_dropNum.clear();
	}

	// this returns the chance for the category to be visited in order to check if
	// drops might come from it.  Category -1 (spoil) must always be visited
	// (but may return 0 or many drops)
	public int getCategoryChance() {
		return _categoryChance;
	}

	/**
	 * useful for seeded conditions...the category will attempt to drop only among
	 * items that are allowed to be dropped when a mob is seeded.
	 * Previously, this only included adena.  According to sh1ny, sealstones are also
	 * acceptable drops.
	 * if no acceptable drops are in the category, nothing will be dropped.
	 * otherwise, it will check for the item's chance to drop and either drop
	 * it or drop nothing.
	 *
	 * @return acceptable drop when mob is seeded, if it exists.  Null otherwise.
	 */
	public FuzeDropData dropSeedAllowedDropsOnly() {
		FastList<FuzeDropData> drops = new FastList<FuzeDropData>();
		int subCatChance = 0;
		for (FuzeDropData drop : getAllDrops()) {
			if ((drop.getItemId() == 57) || (drop.getItemId() == 6360) || (drop.getItemId() == 6361) || (drop.getItemId() == 6362)) {
				drops.add(drop);
				subCatChance += drop.getChance();
			}
		}

		// among the results choose one.
		int randomIndex = Rnd.get(subCatChance);
		int sum = 0;
		for (FuzeDropData drop : drops) {
			sum += drop.getChance();

			if (sum > randomIndex) // drop this item and exit the function
			{
				drops.clear();
				drops = null;
				return drop;
			}
		}
		// since it is still within category, only drop one of the acceptable drops from the results.
		return null;
	}
}
